//
//  ShouCangViewCon.m
//  ShopDemo
//
//  Created by Sunshine on 2018/8/21.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "ShouCangViewCon.h"
#import "ShopDetailController.h"
@interface ShouCangViewCon ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listTableview;
@property(nonatomic,strong)NSMutableArray *datasoce;
@property(nonatomic,assign)NSInteger index;
@property(nonatomic,assign)NSInteger page;

@end

@implementation ShouCangViewCon
-(NSMutableArray *)datasoce{
    if (!_datasoce) {
        _datasoce = [NSMutableArray arrayWithCapacity:10];
    }
    return _datasoce;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.index = 1;
    self.page = 0;
    self.datasoce = [NSMutableArray array];
    NSString *urlstr = [NSString stringWithFormat:@"/dashop/collection/app/list.do?USER_ID=%@&CurrentPage=%ld",[UserDefault objectForKey:LogUserID],self.index];
    NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
    [self ShouCangDataDataMethodWithUrl:endstr];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收藏";
    [self creatTableviewMethod];

    self.listTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.page == 10 ) {
            self.index ++;
            NSString *urlstr = [NSString stringWithFormat:@"/dashop/collection/app/list.do?USER_ID=%@&CurrentPage=%ld",[UserDefault objectForKey:LogUserName],self.index];
            NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
            [self ShouCangDataDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:endstr]];
        }else{
            [self.listTableview.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

-(void)ShouCangDataDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        NSArray *arr = response;
        self.page = arr.count;
        for (NSDictionary *dic in arr) {
            [self.datasoce addObject:dic];
        }
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listTableview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}
-(void)creatTableviewMethod{
    self.listTableview.backgroundColor = [UIColor whiteColor];
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
//    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
}


#pragma mark --UITableViewDelegate,UITableViewDataSource--
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasoce.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    NSDictionary *dic = self.datasoce[indexPath.row];
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIImageView *titimagevi = [UIImageView new];
    NSString *urlstr = [MethodClass returnUrlSetStrWSDLURL:dic[@"GOODS_PIC"]];
    [titimagevi sd_setImageWithURL:[NSURL URLWithString:urlstr]];
    [cell.contentView addSubview:titimagevi];
    titimagevi.sd_layout
    .centerYEqualToView(cell.contentView)
    .leftSpaceToView(cell.contentView, 10*kJLXWidthScale)
    .heightIs(80*kJLXWidthScale)
    .widthIs(70*kJLXWidthScale);
    
    UILabel *priclab = [UILabel new];
    priclab.text = [NSString stringWithFormat:@"￥%.2f",[dic[@"GOODS_MINPRICE"] floatValue]];
    priclab.font = [UIFont systemFontOfSize:13*kJLXWidthScale];
    priclab.textColor = [UIColor redColor];
    [cell.contentView addSubview:priclab];
    priclab.sd_layout
    .bottomSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .leftSpaceToView(titimagevi, 8*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale)
    .rightSpaceToView(cell.contentView, 8*kJLXWidthScale);
    
    UILabel *contentlab = [UILabel new];
    contentlab.text = [NSString stringWithFormat:@"%@",dic[@"GOODS_NAME"]];
    contentlab.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    contentlab.numberOfLines = 0;
    [cell.contentView addSubview:contentlab];
    contentlab.sd_layout
    .bottomSpaceToView(priclab, 5*kJLXWidthScale)
    .leftSpaceToView(titimagevi, 8*kJLXWidthScale)
    .topSpaceToView(cell.contentView, 8*kJLXWidthScale)
    .rightSpaceToView(cell.contentView, 8*kJLXWidthScale);
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 96*kJLXWidthScale;
}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShopDetailController *shopVC = [[ShopDetailController alloc]init];
    NSDictionary *dic = self.datasoce[indexPath.row];
    NSString *urlstr = [NSString stringWithFormat:@"/dashop/goods/app/getGoods?GOODS_ID=%@&USER_ID=%@",dic[@"GOODS_ID"],[UserDefault objectForKey:LogUserID]];
    shopVC.urlStr = urlstr;
    shopVC.usdic = dic;
    [self.navigationController pushViewController:shopVC animated:YES];
    
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSDictionary *dic = self.datasoce[indexPath.row];
        NSString *urlstr = [NSString stringWithFormat:@"/dashop/collection/app/delete.do?COLLECTION_ID=%@&USER_ID=%@&GOODS_ID=%@",dic[@"COLLECTION_ID"],[UserDefault objectForKey:LogUserID],dic[@"GOODS_ID"]];
        NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:urlstr];
        [self deletShouCangJiaMethodWithUrl:endstr];
        [self.datasoce removeObjectAtIndex:indexPath.row];
        [self.listTableview deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.listTableview reloadData];
    }
}

//限时抢购按钮图
-(void)deletShouCangJiaMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        if ([dic[@"RESULT"] integerValue] == 1) {
            [self alerWithMessage:dic[@"MESSAGE"]];
        }
    } failBlock:^(NSError *error) {
        
        
    }];
    
}


// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}


//封装提示框方法
-(void)alerWithMessage:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        //延迟1秒自动消失
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}

@end
